package com.jf.cloud.supplier.feign;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.jf.cloud.api.supplier.feign.SupplierWalletLogOperationFeignClient;
import com.jf.cloud.common.order.bo.EsOrderBO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.supplier.bo.mongo.MongoSupplierWalletLogBO;
import com.jf.cloud.supplier.constant.SupplierWalletChangeReason;
import com.jf.cloud.supplier.constant.SupplierWalletZoneConstant;
import com.jf.cloud.supplier.mapper.SupplierWalletMapper;
import com.jf.cloud.supplier.mapper.SupplierWalletZoneMapper;
import com.jf.cloud.supplier.service.SupplierWalletZoneService;
import com.jf.cloud.supplier.vo.SupplierWalletZoneInfoVO;
import io.swagger.v3.oas.annotations.Hidden;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.stream.Collectors;


/**
 * @author chiley
 * @date 2022/6/30 16:55
 */
@RestController
@Hidden
public class SupplierWalletLogOperationFeignController implements SupplierWalletLogOperationFeignClient {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private SupplierWalletZoneService supplierWalletZoneService;
    @Autowired
    private SupplierWalletMapper supplierWalletMapper;
    @Autowired
    private SupplierWalletZoneMapper supplierWalletZoneMapper;


    @Override
    public ServerResponseEntity<Void> deleteSupplierLog(String orderStr) {
        List<EsOrderBO> orderBOList = JSON.parseArray(orderStr, EsOrderBO.class);
        List<Long> orderIds = orderBOList.stream().map(EsOrderBO::getOrderId).collect(Collectors.toList());
        Criteria criteria = Criteria.where("orderId").in(orderIds).and("reason").is(SupplierWalletChangeReason.PAY.value());
        List<MongoSupplierWalletLogBO> mongoSupplierWalletLogBOS = mongoTemplate.find(new Query(criteria), MongoSupplierWalletLogBO.class);
        if (CollUtil.isNotEmpty(mongoSupplierWalletLogBOS)) {
            // 删除记录
            mongoTemplate.remove(new Query(criteria), MongoSupplierWalletLogBO.class);
            // 退还未结算金额-插入记录的时候增加了未结算金额，这里删除时要减去
            for (MongoSupplierWalletLogBO mongoSupplierWalletLogBO : mongoSupplierWalletLogBOS) {
                Integer zoneNo = null;
                SupplierWalletZoneInfoVO supplierWalletZoneInfo = supplierWalletZoneService.getShopZoneInfo(mongoSupplierWalletLogBO.getSupplierId());
                if (Objects.equals(supplierWalletZoneInfo.getZoneNum(), SupplierWalletZoneConstant.ZERO)) {
                    // 未分片就直接按原来的方式增加(因为添加了唯一索引，所以是不用怕多次加钱)
                    supplierWalletMapper.subUnsettledAmount(mongoSupplierWalletLogBO.getSupplierId(), mongoSupplierWalletLogBO.getChangeAmount());
                } else {
                    // 通过分片增加待结算金额
                    if (Objects.isNull(zoneNo)) {
                        Random random = new Random();
                        zoneNo = random.nextInt(supplierWalletZoneInfo.getZoneNum());
                    }
                    // 如果是支付增加的待结算金额，这里的待结算金额不会为负
                    // 如果是分销进来的这个方法，会根据这笔订单增加的待结算金额的钱包的zoneNo去进行扣减金额，结果也不会是负的
                    supplierWalletZoneMapper.subUnsettledAmount(mongoSupplierWalletLogBO.getSupplierId(), mongoSupplierWalletLogBO.getChangeAmount(), zoneNo);
                }
            }
        }
        return ServerResponseEntity.success();
    }


}
